var time= require("../../utils/util.js");

// pages/forum/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // timeStamp_now:Date.parse(new Date())/1000,
    dataList:[],
    loading: true,
    activetab:0,
    slidenumber : 1,
    tablist: [
      　　　{ v: 0, title: "全部" },
      　　　{ v: 1, title: "表白墙" },
      　　　{ v: 2, title: "学习交流" },
      　　　{ v: 3, title: "生活分享" },
      　　　{ v: 4, title: "ACG" }
    　],
    moveParams: {
      scrollLeft: 0, // scroll-view滚动的距离,默认为0,因为没有触发滚动
      subLeft: 0, //点击元素距离屏幕左边的距离
      subHalfWidth: 0, //点击元素的宽度一半
      screenHalfWidth: 0 //屏幕宽度的一半
    }
  },
  
  getData(num=5,page=0,isTabChange=false){
  
    wx.cloud.callFunction({
      name:"getPostAbstract",
      data:{
        num:num,
        page:page,
        sectionClass:this.data.tablist[this.data.activetab].title
      }
    }).then(res=>{
      console.log(res.result.data);

      var list=res.result.data
      for(var i=0;i<list.length;i++){
        var item_time=time.formatTime(list[i].lastReplytime)
        list[i].time=item_time
      }
      if(isTabChange==false){
        var oldData=this.data.dataList
        var newData=oldData.concat(list)
      }
      else{
        var newData=list
      }
      this.setData({
        dataList:newData
      })
    })
    const that = this;
      setTimeout(()=>{
        that.setData({
          loading:false
        })
      },300)
 
  },
  // 改变分类
  getRect(ele) { //获取点击元素的信息,ele为传入的id
    wx.createSelectorQuery().select(ele).boundingClientRect(
      (rect) => {
        let moveParams = this.data.moveParams; 
        moveParams.subLeft = rect.left;
        moveParams.subHalfWidth = rect.width / 2;
        this.moveTo();
      }).exec()
  },

  handleSwiperNav(e) {
    var isTabChange=false;
    let index = this.data.activetab;   
    this.setData({
      'activetab': e.target.dataset.activetab
    })
    let ele = '#ele' + e.target.dataset.activetab
    this.getRect(ele)
    if(this.data.activetab!=index){
     isTabChange=true;
     this.getData(7,0,isTabChange);
     this.setData({
      loading:true
    })
     }
     
  },
  goToChartRoom(e){
    let uID = e.currentTarget.dataset.id;
    let MyID = wx.getStorageSync('userInfo')._id;
   let id = uID + "" + MyID;
   console.log(id);
    wx.navigateTo({
      url: `/pages/im/room/room?id=${id}`,
    });
  },
  moveTo() {
    let subLeft = this.data.moveParams.subLeft;
    let screenHalfWidth = this.data.moveParams.screenHalfWidth;
    let subHalfWidth = this.data.moveParams.subHalfWidth;
    let scrollLeft = this.data.moveParams.scrollLeft;
    let distance = subLeft - screenHalfWidth + subHalfWidth;

    scrollLeft = scrollLeft + distance;
    // debugger
    this.setData({
      scrollLeft: scrollLeft
    })
  },

  scrollMove(e) {
    this.setData({
      'moveParams.scrollLeft': e.detail.scrollLeft
    })
  },

  onLoad: function () {
    this.getData(8,0,true)
    wx.getSystemInfo({
      success: (res) => {
        let moveParams = this.data.moveParams
        moveParams.screenHalfWidth = res.screenWidth / 2;
        this.setData({
          moveParams
        })
      },
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page=this.data.dataList.length
    this.getData(5,page)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})